<?php

namespace app\api\model;

use common\helper\DateHelper;
use common\model\base\BaseModel;
use common\traits\InstanceTrait;

;


/**
 * Class Plan
 * @package app\api\model
 */
class Plan extends BaseModel
{

    use InstanceTrait;
    protected $table = 'challenge_plan';
    protected $pk = 'plan_id';

    public function getByMember($member_id)
    {
        return $this->where('member_id', $member_id)->select();
    }

    /**
     * 在返回where加上逻辑判断
     * @param $get
     * @return \think\db\Query
     * @throws \common\exception\BaseException
     */
    public function getWhereModel($get)
    {
        $where = $this->getLocalWhere($get);
        return static::where($where);
    }

    /**
     * 常规的where
     * @param $get
     * @throws \common\exception\BaseException
     */
    public function getLocalWhere($get)
    {
        $where = $this->getBaseWhere($get);
        if (!empty($get['finish_time'])) {
            $where[] = ['finish_time', 'between', DateHelper::to_time(DateHelper::day_cycle($get['finish_time']))];
        }
        if (!empty($get['member_id'])) {
            $where[] = ['member_id', '=', $get['member_id']];
        }
        return $where;
    }

    public function getList($get)
    {
        $field = [
            "*",
        ];
        $model = $this->getWhereModel($get);
        $res = $model->select();
        return $res;
    }

    /**
     * @param $where
     * @return array|\PDOStatement|string|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getOne($where)
    {

        $res = $this->where($where)
                    ->find();
        return $res;
    }


}
